package com.JingYan.rocketMq;

import com.JingYan.message.RocketMqMessage;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * RocketMQ 消息发送
 *
 * @author Dong_Jia_Qi on 2022/5/3
 */
@Component
@Slf4j
public class Producer {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public SendResult syncSend(Integer id){
        RocketMqMessage message = new RocketMqMessage();
        message.setId(id);
        log.info(JSONObject.toJSONString(message));
        // 同步发送消息
        return rocketMQTemplate.syncSend(RocketMqMessage.TOPIC, message);
    }
    public void asyncSend(Integer id, SendCallback callback){
        RocketMqMessage message = new RocketMqMessage();
        message.setId(id);
        // 异步发送消息
        rocketMQTemplate.asyncSend(RocketMqMessage.TOPIC, message, callback);
    }

    public void onewaySend(Integer id){
        RocketMqMessage message = new RocketMqMessage();
        message.setId(id);
        // oneway 发送消息
        rocketMQTemplate.sendOneWay(RocketMqMessage.TOPIC, message);
    }


}
